﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Utils;
using System.Data;
using System.Drawing;
using fideeraSoft.Handlers;

namespace fideeraSoft.Paneles
{
    public class PanelAddMateriaPrimaAProduccion:PanelGeneral
    {
        private Button btVolver;
        private TableLayoutPanel tlpBotones;
        private GroupBox gbGral;
        private TableLayoutPanel tlpCantidad;
        private Form formR;
                
        private System.Data.SqlClient.SqlDataAdapter adapter;
        private DataGridView dgvMateriaPrima;
        private DataSet ds;
        private TableLayoutPanel tlpBuscarMateriaPrima;
        private Button btBuscarMateriaPrima;
        private TextBox tbBuscarMateriaPrima;
        private int locationBtCambios;
        private int locationGbBox;
        
        private TextBox tbCant;
        private Label lbCant;
        private Button btAgregar;
        private List<MateriaPrima> lstMateriaPrimaDisp;
        private TableLayoutPanel tlpBuscarMatPrimaLote;
        private Button btBuscarMatPrimLote;
        private TextBox tbBuscarMatPrimaLote;

        private int inLocationTablabuscarMatPrim=0;
        private int inLocationTablabuscarMatPrimLote=0;

        private ListBox lsbMatPrim;
        private List<MateriaPrima> lstMatPrim;

        public PanelAddMateriaPrimaAProduccion(Form form, ListBox lsbMatPrima, List<MateriaPrima> lstMateriaPrima)
        {

            this.formR = form;
            this.lsbMatPrim = lsbMatPrima;
            this.lstMatPrim = lstMateriaPrima;

            InitializeComponent();

            this.formatearPanelSegunForm(formR);
            this.formatearTableTitulo("Agregar Materia Prima a Producción");

            handler = new Handler();



            List<MateriaPrimaXLotes> lstMatPrimaXLote = handler.buscarListaMateriasPrimasXLoteConCantidad();
            

            if (lstMatPrimaXLote != null)
            {
                cargarMateriasPrimasPorLoteEnLista(lstMatPrimaXLote);
                

                dgvMateriaPrima.DataSource = lstMateriaPrimaDisp;


                this.formatearDataGrid();

                this.formatearTablaBuscarMateriaPrima();

                this.formatearTablaBuscarMatPrimaLote();

                this.formatearTablaCantidad();                                

                this.formatearGroupBox();


                this.formatearTablaBotones();


                this.Controls.Add(dgvMateriaPrima);

                this.Controls.Add(tlpBuscarMatPrimaLote);
                this.Controls.Add(tlpBuscarMateriaPrima);

                this.Controls.Add(gbGral);
                
                this.Controls.Add(tlpBotones);

            }
            else 
            {
                MessageBox.Show("No hay lotes con materias primas cargadas.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //NO HAY LOTES MAT
                this.formR.Close();
            }
                                 
        }

        private void cargarMateriasPrimasPorLoteEnLista(List<MateriaPrimaXLotes> lstMatPrimaXLote)
        {
            lstMateriaPrimaDisp = new List<MateriaPrima>();

            MateriaPrima matPrima = new MateriaPrima();

            if (lstMatPrim.Count > 0)
            {

                foreach (MateriaPrimaXLotes matPrimXLot in lstMatPrimaXLote)
                {

                    matPrima = handler.buscarMateriaPrimaPorID(matPrimXLot.MateriaPrima);
                    matPrima.IdLote = matPrimXLot.Lote;
                    matPrima.Cantidad = matPrimXLot.Cantidad;
                    matPrima.Disp = "Si";
                    foreach (MateriaPrima matPrimList in lstMatPrim)
                    {
                        if ((matPrimList.IdMateriaPrima == matPrima.IdMateriaPrima) && (matPrimList.IdLote == matPrima.IdLote))
                        {
                            matPrima.Cantidad = matPrimXLot.Cantidad - matPrimList.Cantidad;
                            matPrima.Disp = "No";
                        }                        
                    }

                    lstMateriaPrimaDisp.Add(matPrima);
                }
            }
            else
            {
                foreach (MateriaPrimaXLotes matPrimXLot in lstMatPrimaXLote)
                {
                    matPrima = handler.buscarMateriaPrimaPorID(matPrimXLot.MateriaPrima);
                    matPrima.Disp = "Si";
                    matPrima.Cantidad = matPrimXLot.Cantidad;
                    matPrima.IdLote = matPrimXLot.Lote;
                    lstMateriaPrimaDisp.Add(matPrima);
                }
            }
        }


        #region FORMATEAR

        private void formatearDataGrid()
        {
            dgvMateriaPrima.AutoGenerateColumns = true;
            dgvMateriaPrima.Size = new Size(this.Size.Width - 100, 350);
            dgvMateriaPrima.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMateriaPrima.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMateriaPrima.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvMateriaPrima.BorderStyle = BorderStyle.Fixed3D;

            dgvMateriaPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMateriaPrima.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaBuscarMateriaPrima()
        {

            inLocationTablabuscarMatPrim = InitialRealPointVertical + dgvMateriaPrima.Size.Height + 20;

            tlpBuscarMateriaPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, tlpBuscarMateriaPrima.Size.Width), inLocationTablabuscarMatPrim);

            tlpBuscarMateriaPrima.Controls.Add(btBuscarMateriaPrima);
            tlpBuscarMateriaPrima.Controls.Add(tbBuscarMateriaPrima);
        }



        private void formatearTablaBuscarMatPrimaLote()
        {

            inLocationTablabuscarMatPrimLote = inLocationTablabuscarMatPrim+tlpBuscarMateriaPrima.Size.Height+5;

            tlpBuscarMatPrimaLote.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, tlpBuscarMateriaPrima.Size.Width), inLocationTablabuscarMatPrimLote);

            tlpBuscarMatPrimaLote.Controls.Add(btBuscarMatPrimLote);
            tlpBuscarMatPrimaLote.Controls.Add(tbBuscarMatPrimaLote);
        }       

        private void formatearGroupBox()
        {
           // gbGral.Controls.Add(tlpBuscarMateriaPrima);

            gbGral.Controls.Add(tlpCantidad);

            locationGbBox = inLocationTablabuscarMatPrimLote + tlpBuscarMateriaPrima.Size.Height + 20;
            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, gbGral.Size.Width), locationGbBox);
                      
        }

        private void formatearTablaCantidad()
        {
            tlpCantidad.Controls.Add(lbCant);

            tlpCantidad.Controls.Add(tbCant);

            tlpCantidad.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpCantidad.Size.Width), 35);

        }        


        private void formatearTablaBotones()
        {                       

            locationBtCambios = locationGbBox + gbGral.Size.Height + 20;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.formR.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btAgregar);

        }

        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.btVolver = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.tlpCantidad = new System.Windows.Forms.TableLayoutPanel();
            this.dgvMateriaPrima = new System.Windows.Forms.DataGridView();
            this.tlpBuscarMateriaPrima = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarMateriaPrima = new System.Windows.Forms.Button();
            this.tbBuscarMateriaPrima = new System.Windows.Forms.TextBox();
            this.tbCant = new System.Windows.Forms.TextBox();
            this.lbCant = new System.Windows.Forms.Label();
            this.btAgregar = new System.Windows.Forms.Button();
            this.tlpBuscarMatPrimaLote = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarMatPrimLote = new System.Windows.Forms.Button();
            this.tbBuscarMatPrimaLote = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMateriaPrima)).BeginInit();
            this.SuspendLayout();
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // gbGral
            // 
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(400, 100);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Cantidad Materia Prima";
            // 
            // tlpCantidad
            // 
            this.tlpCantidad.BackColor = System.Drawing.Color.LightCyan;
            this.tlpCantidad.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpCantidad.ColumnCount = 2;
            this.tlpCantidad.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantidad.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpCantidad.Location = new System.Drawing.Point(0, 0);
            this.tlpCantidad.Name = "tlpCantidad";
            this.tlpCantidad.RowCount = 1;
            this.tlpCantidad.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpCantidad.Size = new System.Drawing.Size(250, 40);
            this.tlpCantidad.TabIndex = 0;
            // 
            // dgvMateriaPrima
            // 
            this.dgvMateriaPrima.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.dgvMateriaPrima.Name = "dgvMateriaPrima";
            this.dgvMateriaPrima.ReadOnly = true;
            this.dgvMateriaPrima.Size = new System.Drawing.Size(240, 350);
            this.dgvMateriaPrima.TabIndex = 0;
            this.dgvMateriaPrima.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvLotes_CellContentClick);
            // 
            // tlpBuscarMateriaPrima
            // 
            this.tlpBuscarMateriaPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarMateriaPrima.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMateriaPrima.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMateriaPrima.ColumnCount = 2;
            this.tlpBuscarMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMateriaPrima.Name = "tlpBuscarMateriaPrima";
            this.tlpBuscarMateriaPrima.RowCount = 1;
            this.tlpBuscarMateriaPrima.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarMateriaPrima.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarMateriaPrima.TabIndex = 0;
            // 
            // btBuscarMateriaPrima
            // 
            this.btBuscarMateriaPrima.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarMateriaPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMateriaPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMateriaPrima.Name = "btBuscarMateriaPrima";
            this.btBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.btBuscarMateriaPrima.TabIndex = 0;
            this.btBuscarMateriaPrima.Text = "Buscar Materia Prima (nombre)";
            this.btBuscarMateriaPrima.UseVisualStyleBackColor = false;
            this.btBuscarMateriaPrima.Click += new System.EventHandler(this.btBuscarMateriaPrima_Click);
            // 
            // tbBuscarMateriaPrima
            // 
            this.tbBuscarMateriaPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tbBuscarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMateriaPrima.MaxLength = 50;
            this.tbBuscarMateriaPrima.Name = "tbBuscarMateriaPrima";
            this.tbBuscarMateriaPrima.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarMateriaPrima.TabIndex = 0;
            // 
            // tbCant
            // 
            this.tbCant.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCant.Location = new System.Drawing.Point(0, 0);
            this.tbCant.MaxLength = 5;
            this.tbCant.Name = "tbCant";
            this.tbCant.Size = new System.Drawing.Size(80, 30);
            this.tbCant.TabIndex = 0;
            this.tbCant.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // lbCant
            // 
            this.lbCant.AutoSize = true;
            this.lbCant.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCant.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCant.Location = new System.Drawing.Point(0, 0);
            this.lbCant.Name = "lbCant";
            this.lbCant.Size = new System.Drawing.Size(15, 30);
            this.lbCant.TabIndex = 0;
            this.lbCant.Text = "Cantidad";
            this.lbCant.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btAgregar
            // 
            this.btAgregar.BackColor = System.Drawing.Color.AliceBlue;
            this.btAgregar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btAgregar.Location = new System.Drawing.Point(0, 0);
            this.btAgregar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btAgregar.Name = "btAgregar";
            this.btAgregar.Size = new System.Drawing.Size(75, 40);
            this.btAgregar.TabIndex = 0;
            this.btAgregar.Text = "Agregar";
            this.btAgregar.UseVisualStyleBackColor = false;
            this.btAgregar.Click += new System.EventHandler(this.btAgregar_Click);
            // 
            // tlpBuscarMatPrimaLote
            // 
            this.tlpBuscarMatPrimaLote.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarMatPrimaLote.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMatPrimaLote.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMatPrimaLote.ColumnCount = 2;
            this.tlpBuscarMatPrimaLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrimaLote.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrimaLote.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMatPrimaLote.Name = "tlpBuscarMatPrimaLote";
            this.tlpBuscarMatPrimaLote.RowCount = 1;
            this.tlpBuscarMatPrimaLote.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarMatPrimaLote.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarMatPrimaLote.TabIndex = 0;
            // 
            // btBuscarMatPrimLote
            // 
            this.btBuscarMatPrimLote.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarMatPrimLote.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMatPrimLote.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMatPrimLote.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMatPrimLote.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMatPrimLote.Name = "btBuscarMatPrimLote";
            this.btBuscarMatPrimLote.Size = new System.Drawing.Size(200, 30);
            this.btBuscarMatPrimLote.TabIndex = 0;
            this.btBuscarMatPrimLote.Text = "Buscar Materia Prima (idLote)";
            this.btBuscarMatPrimLote.UseVisualStyleBackColor = false;
            this.btBuscarMatPrimLote.Click += new System.EventHandler(this.btBuscarMatPrimLote_Click);
            // 
            // tbBuscarMatPrimaLote
            // 
            this.tbBuscarMatPrimaLote.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMatPrimaLote.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMatPrimaLote.MaxLength = 6;
            this.tbBuscarMatPrimaLote.Name = "tbBuscarMatPrimaLote";
            this.tbBuscarMatPrimaLote.Size = new System.Drawing.Size(100, 30);
            this.tbBuscarMatPrimaLote.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvMateriaPrima)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion


        private void btVolver_Click(object sender, EventArgs e)
        {

            this.formR.Close();
        }

      

        private void btAgregar_Click(object sender, EventArgs e)
        {
            int rowIndex =-1;

            try
            {
                if ((dgvMateriaPrima.Rows.Count > 0) && (tbCant.Text.Length > 0))
                {
                    rowIndex = dgvMateriaPrima.CurrentCell.RowIndex;


                    if (rowIndex > -1)
                    {                        
                        double res;
                        if (Double.TryParse(tbCant.Text.ToString().Trim(), out res))
                        {
                            MateriaPrima matPrima = new MateriaPrima();

                            if((dgvMateriaPrima.Rows[rowIndex].Cells[8].Value!=null)&&(dgvMateriaPrima.Rows[rowIndex].Cells[8].Value.ToString().Equals("No")))
                            {
                                MessageBox.Show("La materia prima seleccionada ya esta en uso. Deberá salir y remover la preseleccionada para luego agregar la cantidad necesaria.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                tbCant.Clear();
                                return;
                            }

                            matPrima.Cantidad = (float)Convert.ToDouble(tbCant.Text);
                            float cantMatTab=(float)Convert.ToDouble(dgvMateriaPrima.Rows[rowIndex].Cells[6].Value.ToString().Trim());
                            if (cantMatTab >= matPrima.Cantidad)
                            {
                                matPrima.IdMateriaPrima = Convert.ToInt32(dgvMateriaPrima.Rows[rowIndex].Cells[0].Value.ToString());
                                matPrima.IdLote = Convert.ToInt32(dgvMateriaPrima.Rows[rowIndex].Cells[1].Value.ToString());
                                matPrima.Nombre = dgvMateriaPrima.Rows[rowIndex].Cells[2].Value.ToString();
                                

                                if (string.IsNullOrWhiteSpace(dgvMateriaPrima.Rows[rowIndex].Cells[5].Value.ToString())) matPrima.Codigo = ""; else matPrima.Codigo = dgvMateriaPrima.Rows[rowIndex].Cells[5].Value.ToString();
                                if (string.IsNullOrEmpty(dgvMateriaPrima.Rows[rowIndex].Cells[7].Value.ToString())) matPrima.Unidad = ""; else matPrima.Unidad = dgvMateriaPrima.Rows[rowIndex].Cells[7].Value.ToString();

                                string strMatPrim = "Id: " + matPrima.IdMateriaPrima + " - " + matPrima.Nombre + " - " + matPrima.Cantidad + " · " + matPrima.Unidad + " | Lote: "+matPrima.IdLote;

                                //MessageBox.Show("ITEM: "+strMatPrim, "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    //DEBUG

                                lsbMatPrim.Items.Add(strMatPrim);
                                lstMatPrim.Add(matPrima);

                                formR.Close();

                            }
                            else
                            {
                                MessageBox.Show("La cantidad de materia prima ingresada es mayor a la disponible. Vuelva a ingresar correctamente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                tbCant.Clear();

                            }



                            //MessageBox.Show("RAZON SOCIAL: " +prov.RazonSocial, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        }
                        else
                        {
                            MessageBox.Show("Debe ingresar datos numéricos para la cantidad de materia prima a utilizar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            tbCant.Clear();
                        }

                    }
                    else
                    {
                        MessageBox.Show("No hay celda seleccionada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("No hay datos cargados.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception exc)
            {
                tbCant.Clear();
                CLogger.Append("[btAgregar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }

        private void dgvMateriaPrima_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMateriaPrima.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void dgvLotes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMateriaPrima.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btBuscarMateriaPrima_Click(object sender, EventArgs e)
        {
            dgvMateriaPrima.ClearSelection();

            if (!string.IsNullOrWhiteSpace(tbBuscarMateriaPrima.Text))
            {
                string searchValue = tbBuscarMateriaPrima.Text;

                int inRowFinded = -1;  
                try
                {
                    foreach (DataGridViewRow row in dgvMateriaPrima.Rows)
                    {
                        if (row.Cells[2].Value.ToString().Equals(searchValue))
                        {
                            dgvMateriaPrima.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;
                            inRowFinded = row.Index;
                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMateriaPrima.CurrentCell = row.Cells[0];
                            tbBuscarMateriaPrima.Clear();
                            break;

                        }

                    }
                    if(inRowFinded==-1)
                    {
                        MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        tbBuscarMateriaPrima.Clear();
                    }
                }
                catch (Exception exc)
                {
                    tbBuscarMateriaPrima.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NOMBRE DE MATERIA PRIMA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btBuscarMatPrimLote_Click(object sender, EventArgs e)
        {
            dgvMateriaPrima.ClearSelection();

            if (!string.IsNullOrWhiteSpace(tbBuscarMatPrimaLote.Text))
            {
                string searchValue = tbBuscarMatPrimaLote.Text;

                int inRowFinded = -1;  
                try
                {
                    foreach (DataGridViewRow row in dgvMateriaPrima.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvMateriaPrima.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;
                            inRowFinded = row.Index;
                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMateriaPrima.CurrentCell = row.Cells[0];
                            tbBuscarMatPrimaLote.Clear();
                            break;

                        }

                    }

                    if (inRowFinded == -1)
                    {
                        MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        tbBuscarMatPrimaLote.Clear();
                    }

                }
                catch (Exception exc)
                {
                    tbBuscarMatPrimaLote.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO ID LOTE A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }


    }
}
